package client;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import core.ResourceType;

import java.awt.Graphics;
import java.awt.Color;
import java.lang.Math;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * 
 * @author derekChou, andrewChen
 * 
 * 
 *         F period andrewtheannihilator@gmail.com lolatdc749@gmail.com
 * 
 */
// TODO better name, new class for entire thing
// finite number of each building type

public class BuildTray extends JComponent {
	private final int DISPLAY_WIDTH = 150;
	private final int DISPLAY_HEIGHT = 100;

	public static final int BUFFER = 10;
	public static final int XSET = BUFFER;
	public static final int YSET = BUFFER + BUFFER;
	public static final int YINC = 25;
	public static final int XINC = 100;
	public static final int BOX_SIZE = 15;
	public static final int BOX_SPACE = 3;
	public static final int BUTTON_SIZE = 35;

	private final int BUILDING_TYPE;
	private final String NAME;
	private final int NUM_RESOURCES;
	private int[] resource_counts = new int[5];// TODO ORDER OF SETUP WITH ENUM
	// BRICK, LUMBER, WOOL, ORE, GRAIN;
	private Color[] resource_colors = { new Color(255, 0, 0),
			new Color(205, 133, 63), new Color(124, 252, 0), Color.LIGHT_GRAY,
			Color.YELLOW };

	// TODO look for that enum
	private final Color color;

	public BuildTray(int buildingType, Color c) {
		this.BUILDING_TYPE = buildingType;

		switch (buildingType) {
		case 0:
			NAME = "Road";
			NUM_RESOURCES = 2;
			resource_counts[0] = 1;
			resource_counts[1] = 1;
			resource_counts[2] = 0;
			resource_counts[3] = 0;
			resource_counts[4] = 0;
			break;
		case 1:
			NAME = "Settlement";
			NUM_RESOURCES = 4;
			resource_counts[0] = 1;
			resource_counts[1] = 1;
			resource_counts[2] = 1;
			resource_counts[3] = 0;
			resource_counts[4] = 1;
			break;
		case 2:
			NAME = "City";
			NUM_RESOURCES = 2;
			resource_counts[0] = 0;
			resource_counts[1] = 0;
			resource_counts[2] = 0;
			resource_counts[3] = 3;
			resource_counts[4] = 2;
			break;
		case 3:
			NAME = "Card";
			NUM_RESOURCES = 3;
			resource_counts[0] = 0;
			resource_counts[1] = 0;
			resource_counts[2] = 1;
			resource_counts[3] = 1;
			resource_counts[4] = 1;
			break;
		default:
			NAME = "WTF";
			NUM_RESOURCES = -1;
			resource_counts[0] = -1;
			resource_counts[1] = -1;
			resource_counts[2] = -1;
			resource_counts[3] = -1;
			resource_counts[4] = -1;
		}
		color = c;

		init();
	}

	public void init() {
		setBackground(Color.WHITE);
		setSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
		// addMouseListener(this);
		// addMouseMotionListener(this);
		repaint();

	}

	public void paint(Graphics g) {
		// final int TIME_BETWEEN_REPLOTS = 500; // change to your liking
		g.setColor(color);
		g.fillRect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT);
		// g.setColor(Color.black);
		// g.drawString("LOL", 0, 0);
		paintName(g);
		paintReqs(g);
		paintButton(g);
	}

	/*
	 * TODO center
	 */
	private void paintName(Graphics g) {
		g.setColor(Color.black);
		g.drawString(NAME, BUFFER + 65, YSET);
		// System.out.println(this.getAlignmentX());

	}

	/*
	 * TODO center
	 */
	private void paintReqs(Graphics g) {

		int toPaint = NUM_RESOURCES;
		int resource = 0;
		int x = 0;
		while (toPaint > 0) {
			int num_of_resource = resource_counts[resource];
			if (num_of_resource != 0) {
				paintBox(g, num_of_resource, XSET + x++
						* (BOX_SPACE + BOX_SIZE), num_of_resource,
						resource_colors[resource]);
				toPaint--;
			}
			resource++;
		}

		// for (int i = 0; i < NUM_RESOURCES; i++) {
		// if (resource_counts[i] != 0) {
		// // TODO make a counter
		// paintBox(g, resource_counts[i], XSET + i
		// * (BOX_SPACE + BOX_SIZE), 8);
		// }
		// // paintBox(g, resource_counts[i], XSET + i * (BOX_SPACE +
		// // BOX_SIZE));
		// // TODO order of resource_counts: resource_counts[i] will not work!
		// }
	}

	private void paintButton(Graphics g) {
		// TODO get icons
		// TODO button location, button size
		// TODO set the color based ont BUILDING_TYPE
		g.setColor(Color.BLACK);
		g.drawRect(57, 50, BUTTON_SIZE, BUTTON_SIZE);
		// g.setColor(C)
		g.fillRect(57, 50, BUTTON_SIZE, BUTTON_SIZE);

		// ImageIcon buildButtonIcon = createImageIcon("./images/x.gif");

		// JButton jb = new JButton();
		// jb.setActionCommand("build");
		JButton b;
		switch (BUILDING_TYPE) {
		case 0:
			b = new BuildRoadButton();
			break;
		case 1:
			b = new BuildSettlementButton();
			break;
		case 2:
			b = new BuildCityButton();
			break;
		case 3:
			b = new BuildCardButton();
			break;
		default:
			b = null;
		}
		// b = new BuildRoadButton();
		this.add(b);
		b.setBounds(57, 50, BUTTON_SIZE, BUTTON_SIZE);

	}

	private void paintBox(Graphics g, int resource, int x, int number, Color c) {
		g.setColor(c);// TODO replace color with icon perhaps
		g.fillRect(x, YINC, BOX_SIZE, BOX_SIZE);
		g.setColor(Color.BLACK); // TODO better colors
		// TODO get the number centered
		g.drawString("" + number, x, YINC + 10);
	}

	public static void main(String[] aaaaaargs) {
		final int FRAME_WIDTH = 600;
		final int MENUBAR_HEIGHT = 34;
		final int FRAME_HEIGHT = 100 + MENUBAR_HEIGHT;

		JFrame f = new JFrame();
		f.setLayout(null);
		f.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setTitle("hi");

		BuildTray[] buildtrays = new BuildTray[4];

		for (int i = 0; i < buildtrays.length; i++) {
			buildtrays[i] = new BuildTray(i, Color.cyan);
			f.add(buildtrays[i]);
			buildtrays[i].setLocation(150 * i, 0);
		}

		// for (int building_type = 0; building_type < 4; building_type++) {
		// f.add(new BuildTray(0, 0, building_type, Color.GREEN));
		// }

		// JButton b = new BuildRoadButton();
		//
		// f.add(b);
		// b.setBounds(50, 50, 50, 50);
		// b.setVisible(true);
		f.setVisible(true);

	}

	public static abstract class BuildButton extends JButton implements
			ActionListener {

		public BuildButton() {
			this(57, 50, BuildTray.BUTTON_SIZE, BuildTray.BUTTON_SIZE);
		}

		public BuildButton(int x, int y, int width, int height) {
			super("BUILD!"); // TODO make this an icon

			addActionListener(this);

		}

		public void actionPerformed(ActionEvent e) {
			build();
			// if ("road".equals(e.getActionCommand())) {
			// System.out.println("road");
			// } else if ("settlement".equals(e.getActionCommand())) {
			//
			// System.out.println("settlement");
			// } else if ("city".equals(e.getActionCommand())) {
			//
			// System.out.println("city");
			// } else if ("card".equals(e.getActionCommand())) {
			//
			// System.out.println("building");
			// }
		}

		public void build() {
			System.out.println("Build!");
		}

	}

	public static class BuildRoadButton extends BuildButton implements
			ActionListener {
		public void build() {
			System.out.println("Road!");
		}
	}

	public static class BuildSettlementButton extends BuildButton implements
			ActionListener {
		public void build() {
			System.out.println("Settlement!");
		}
	}

	public static class BuildCityButton extends BuildSettlementButton implements
			ActionListener {
		public void build() {
			System.out.println("City!");
		}
	}

	public static class BuildCardButton extends BuildSettlementButton implements
			ActionListener {
		public void build() {
			System.out.println("Card!");
		}
	}

}
